#ifndef __GAMEOBJECT_H__
#define __GAMEOBJECT_H__

#include "stdafx.h"
#include "../FileSystem/IAttributes.h"
#include "vector2d.h"
#include "line2d.h"
#include "CollisionArea.h"
#include "irrArray.h"
#include "Box2D.h"

enum
{
	Object_None = -1,
	Object_Balloon,
	Object_Robot,
	Object_Room,
	Object_Cinematic,
	Object_ThrowableObj,
	Object_BaseBot,
};


class ScreenManager;
class ASprite;
class CRect;
class b2Body;
class GameObject
{

public:
	static int sRandomID;
	GameObject();
	~GameObject(void);

	int m_objectID;

	struct Flag
	{
		bool Enabled:1;
		bool Updatable:1;
		bool Renderable:1;
		bool InFarRange:1;
		bool NeedDespawn:1;
	};
	Flag	m_flags;
	inline bool    IsEnabled() const { return m_flags.Enabled ; }
	inline bool    IsUpdatable() const { return m_flags.Updatable && IsEnabled(); }
	inline bool    IsRenderable() const { return m_flags.Renderable && IsEnabled(); }
	inline bool    IsInFarRange() const { return m_flags.InFarRange; }  
	inline bool    IsNeedDespawn() const { return m_flags.NeedDespawn; }  

	virtual void   SetEnabled(bool v);
	inline void	   SetUpdatable(bool v) { m_flags.Updatable = v; }
	inline void    SetRenderable(bool v) { m_flags.Renderable = v; }
	inline void    SetInFarRange(bool v) { m_flags.InFarRange = v; }
	inline void    UnSpawn() { m_flags.NeedDespawn = true; }

	bool                 m_bIntersectionListRemoveEvent;
	void AddContact( GameObject* h );
	void AddCollision( GameObject* h, const b2Vec2& position, const b2Vec2& normal, float impulseMagnitude );

	void MasterUpdate_PreCollision();
	virtual void Update_PreCollision() {}
	virtual void Update_PostCollision() {}
	virtual void ResolveCollisions(){}
	virtual void TestCollisions(GameObject* obj,vector2df* resultant){}

	void SetPosition(float x, float y);
	void SetSpeed(const b2Vec2& v);
	//void SetPosition(const vector2df& pos) {m_position = pos;}
	//inline		vector2df	 GetPosition() { return m_position; }

private:
	
protected:
	struct Contact
	{
		GameObject* obj;
		b2Vec2 position;
		b2Vec2 normal;
		float    magnitude;
	};
	typedef array<Contact> CollisionList;

	CollisionList        m_collisionList;

	typedef array<GameObject*> GameObjectHandleList;
	bool                 m_doTrackContactList;
	GameObjectHandleList m_contactList;

	float	m_speedUnit;

	//vector2df m_position;
	b2Body*            m_physicsBody;

	int m_objectType;

		


public:
	virtual void Draw();
	virtual void Update();
	virtual void Initialize(){}
	virtual void TakeDamage(int damage){}

	virtual void SaveData(CWriteFile* stream);
	virtual void LoadData(CReadFile* stream);

	inline int GetObjectType(){return m_objectType;}
	inline void SetObjectType(int type){m_objectType = type;}

	virtual void LoadParams(IAttributes* attr);
	virtual void Load(IAttributes* attr){}

	virtual bool ShouldCollideWith( GameObject* h ) { return true; }

	virtual void InitPhysics(){}
};

#endif